using System;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class UI_Option : MonoBehaviour
{
    [SerializeField] private Toggle displayHealthBarToggle;
    [SerializeField] private AudioMixer audioMixer;
    [SerializeField] private float mixerMultiplier = 25;

    private Player player;
    [Header("BGM Volume Setting")]
    [SerializeField] private Slider bgmSlider;
    [SerializeField] private string bgmParametr;

    [Header("SFX Volume Setting")]
    [SerializeField] private Slider sfxSlider;
    [SerializeField] private string sfxParametr;

    private void OnEnable()
    {
        LoadUpVolume();
    }
    private void OnDisable()
    {
        PlayerPrefs.SetFloat(sfxParametr, sfxSlider.value);
        PlayerPrefs.SetFloat(bgmParametr, bgmSlider.value);
    }

    public void LoadUpVolume()
    {
        sfxSlider.value = PlayerPrefs.GetFloat(sfxParametr, .6f);
        bgmSlider.value = PlayerPrefs.GetFloat(bgmParametr, .6f);
    }
    public void Start()
    {
        player = FindAnyObjectByType<Player>();
        displayHealthBarToggle.isOn = true;
        displayHealthBarToggle.onValueChanged.AddListener(OnHealthBarToggleChange);
    }
    private void OnHealthBarToggleChange(bool isOn)
    {
        player.playerHealth.EnableHealthBarInHead(isOn);
    }

    public void BGMSliderValue(float value)
    {
        float newValue = MathF.Log10(value) * mixerMultiplier;
        audioMixer.SetFloat(bgmParametr, newValue);
    }
    public void SFXSliderValue(float value)
    {
        float newValue = MathF.Log10(value) * mixerMultiplier;
        audioMixer.SetFloat(sfxParametr, newValue);
    }
    public void GoGameMenu()
    {
        Time.timeScale = 1f;
        SaveManager.Instance.SaveGame();
        GameManager.Instance.LoadScene("Main Menu", RespawnType.None);
    }
}
